import cv2 as cv
import numpy as np
from PyCmpltrtok.common import sep


def getHarrisCorners(img, thresh, blockSize=3):
    cornerStrength = cv.cornerHarris(img, blockSize=blockSize, ksize=3, k=0.01)
    ret = cv.threshold(cornerStrength, thresh, 255, cv.THRESH_BINARY)
    print(ret[0], ret[1].shape)
    harrisCorners = ret[1].astype(np.uint8)
    return harrisCorners


if '__main__' == __name__:
    import sys

    def _main():
        sep('Load img')
        path = r'D:\_const\large_data\pic\harris_corner\chuch.jpg'
        argc = len(sys.argv)
        if argc >= 2:
            path = sys.argv[1]
        img = cv.imread(path, cv.IMREAD_GRAYSCALE)

        sep('Harris')
        for m in range(4, 0, -1):
            thresh = 0.1 ** m
            sep(thresh)
            harrisCorners = getHarrisCorners(img, thresh)

            print('Showing ...')
            cv.imshow(f'Harris {thresh}', harrisCorners)

        print('Close the image windows to continue ...')
        cv.waitKey(0)
        cv.destroyAllWindows()

    _main()
    sep('All Over')
